﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    ForumInterface posty,tematy,watki,location,nowyTemat,nowyPost;
    ForumDataContext d1 =  new ForumDataContext();
    LogowanieInterface log;
    LoadData load;
    SendData send;
    static int id_topic,id_thread =1,userID;

    protected void Page_Load(object sender, EventArgs e)
    {
        log = new LogowanieInterface(Panel3);
        load = new LoadData(d1);
        userID = log.GetSetUserID;

        posty = new ForumInterface(Panel1);
        posty.GetSetInterfaceType = "postView";
        posty.InsertPosts(load.ReturnPosts(id_topic));
        posty.DrawInterface();

        tematy = new ForumInterface(Panel2);
        tematy.GetSetInterfaceType = "topicView";
        tematy.InsertTopics(load.ReturnTopics(id_thread));
        tematy.DrawInterface();

        watki = new ForumInterface(Panel3);
        watki.GetSetInterfaceType = "threadView";
        watki.InsertThread(load.ReturnThreads());
        watki.DrawInterface();

        location = new ForumInterface(LocationPanel);
        location.GetSetInterfaceType = "locationView";
        location.SetThread(load.ReturnCurrentThread(id_thread));
        location.SetTopic(load.ReturnCurrentTopic(id_topic));
        location.DrawInterface();

        nowyTemat = new ForumInterface(Panel2b);
        nowyTemat.GetSetInterfaceType = "topicAddingView";
        nowyTemat.DrawInterface();

        nowyPost = new ForumInterface(Panel1b);
        nowyPost.GetSetInterfaceType = "postAddingView";
        nowyPost.DrawInterface();

        ButtonsHandling();
    }



    void ButtonsHandling()
    {
        if (nowyPost.sendButton != null) nowyPost.sendButton.Command += new CommandEventHandler(Btn_SendPost);
        if (nowyTemat.sendButton != null) nowyTemat.sendButton.Command += new CommandEventHandler(Btn_SendTopic);
        foreach (Button b in tematy.linkButton) b.Command += new CommandEventHandler(Btn_SelectedTopic);
        foreach (Button b in watki.linkButton) b.Command += new CommandEventHandler(Btn_SelectedThread);
        
    }

    void Btn_SendPost(object sender, CommandEventArgs e)
    {
        send = new SendData(d1);
  
       
        send.SendPost(id_topic, userID, nowyPost.GetSetNewPostText);
        
        Response.Redirect(Request.Url.ToString());
    }

    void Btn_SendTopic(object sender, CommandEventArgs e)
    {
        send = new SendData(d1);
        send.SendTopic(id_thread, userID, nowyTemat.GetSetNewTopicText, nowyTemat.GetSetFirstPostText);
        Response.Redirect(Request.Url.ToString());
       // TextBox1.Text = nowyTemat.GetSetNewTopicText+ " " + nowyTemat.GetSetFirstPostText;
    }

    void Btn_SelectedTopic(object sender, CommandEventArgs e)
    {
        id_topic = Int32.Parse(((Button)sender).CommandArgument);
        Response.Redirect(Request.Url.ToString());
    }

    void Btn_SelectedThread(object sender, CommandEventArgs e)
    {
        id_thread = Int32.Parse(((Button)sender).CommandArgument);
        id_topic = -1;
        Response.Redirect(Request.Url.ToString());
    }

}